import request from "@/axios";
import globle from "@/common/global"
export default ({
  namespaced:true,
  state: {
    //文章详情数据
    ArticleDetail:null,

    //推荐文章信息
    ArticleRecommend:[],

    //目录
    ArticleDetailCatalog:[],
    CatalogNowId:0,//当前目录ID


    //设置组件状态
    ArticleDetailSetState:false,
    //权限组件状态
    ArticleDetailAuthorityState:false,


  },
  getters: {},
  mutations: {

    SETARTICLEDETAIL_:(state,data) => state.ArticleDetail = data ,
    //设置文章详情
    SETARTICLEDETAIL:(state,{ArticleDetail,ArticleRecommend}) => {

        //设置文章详情
      	ArticleDetail.image=ArticleDetail.image?.split(',')//获取图片列表

        ArticleDetail.oldContent = ArticleDetail.content
        if(ArticleDetail.classify==1 && ArticleDetail.content)
        ArticleDetail.content=globle.getContent(ArticleDetail.content) //解析内容
        state.ArticleDetail=ArticleDetail
        

        //设置推荐文章
         ArticleRecommend?.forEach(item =>{
          item.image=item.image?.split(',')//获取图片列表
          let image = item.image ? item.image[0] : null //上传的照片
          item.cover= item.cover || image || item.headphoto
        }) 
        state.ArticleRecommend = ArticleRecommend


    },
     //设置目录
     SETARTICLEDETAILCATALOG(state,data){
      state.ArticleDetailCatalog = []
       data.forEach(item =>{
             item.id = state.ArticleDetailCatalog.length
             state.ArticleDetailCatalog.push(item)
        })
    },
    //设置设置组件状态
    SETARTICLEDETAILSETSTATE:(state,flag) => state.ArticleDetailSetState = flag,
    //设置权限组件状态
    SETARTICLEDETAILAUTHORITY:(state,flag) => state.ArticleDetailAuthorityState = flag,

    ARTICLEDETAILSETDOWN:state => state.ArticleDetailSetState = false ,
    ARTICLEDETAILAUTHORITYDOWN:state => state.ArticleDetailAuthorityState = false ,
    
     //设置当前目录id
     SETCATALOGNOWID:(state,id) => state.CatalogNowId = id,
  },
  actions:{
    async GetArticleDetail(context,{id, createdUid}){ //获取详情
        try{
           let res =await request.get("/api/article/detail",{params:{id, createdUid}})
    
           if(res.data.code == 200) 
            context.commit('SETARTICLEDETAIL',res.data.data)
           return res.data
        }catch(error){return {message:error,code:400}}
    }
  },
})